package com.zhoug.map2.location;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import com.zhoug.logger2.Logger;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

/**
 * @Author: zhoug
 * @Date: 2025-01-07
 * @Description:
 */
public class GeocoderHelper {
    private static final String TAG = ">>>GeocoderHelper";
    private static final boolean DEBUG=false;

    /**
     * 通过经纬度获取地址信息
     * 同步调用
     * @param context
     * @param latitude
     * @param longitude
     * @return
     */
    public static ZAddress getAddressFromLocationSync(Context context, double latitude, double longitude) {
        Geocoder geocoder = new Geocoder(context, Locale.SIMPLIFIED_CHINESE);
        List<Address> addresses;
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses != null && !addresses.isEmpty()) {
                Address address = addresses.get(0);
               // public final String ;//国家:中国
                //    public final String ;//国家代码:GCJ02
                //    public final String province;//省份:重庆市
                //    public final String ;//城市:重庆市
                //    public final String cityCode;//城市代码:023
                //    public final String district;//区:渝中区
                //    public final String ;//街道:美专校街
                //    public final String ;//街道号码:9号
                //    public final String ;//地址:中国重庆市渝中区美专校街9号
                //    public final String adcode;//500103
                if (DEBUG) {
                    Logger.d(TAG, "getLocality:"+address.getLocality());
                    Logger.d(TAG, "getAddressLine:"+address.getAddressLine(0));
                    Logger.d(TAG, "getAdminArea:"+address.getAdminArea());
                    Logger.d(TAG, "getCountryCode:"+address.getCountryCode());
                    Logger.d(TAG, "getCountryName:"+address.getCountryName());
                    Logger.d(TAG, "getFeatureName:"+address.getFeatureName());
                    Logger.d(TAG, "getPhone:"+address.getPhone());
                    Logger.d(TAG, "getPostalCode:"+address.getPostalCode());
                    Logger.d(TAG, "getPremises:"+address.getPremises());
                    Logger.d(TAG, "getSubAdminArea:"+address.getSubAdminArea());
                    Logger.d(TAG, "getSubLocality:"+address.getSubLocality());
                    Logger.d(TAG, "getSubThoroughfare:"+address.getSubThoroughfare());
                    Logger.d(TAG, "getThoroughfare:"+address.getThoroughfare());
                }
                ZAddress zAddress = new ZAddress.Builder()
                        .latitude(latitude)
                        .longitude(longitude)
                        .country(address.getCountryName())
                        .countryCode(address.getCountryCode())
                        .city(address.getAdminArea())
                        .street(address.getThoroughfare())
                        .streetNumber(address.getSubThoroughfare())
                        .address(address.getAddressLine(0))
                        .build();
                return zAddress;
            }
        } catch (IOException e) {
           Logger.e(TAG, "getAddressFromLocationSync:"+e.getMessage());
        }
        return null;
    }
}
